import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Sort {
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length ; i++) {
            int tmp =array[i];
            int j =i-1;
            for (; j >=0 ; j--) {
                if (array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    public static void shellSort(int[] array){
        int gap =array.length;
        while (gap>1){
            gap/=2;
            shell(array,gap);
        }
    }

    private static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length ; i++) {
            int tmp =array[i];
            int j =i-gap;
            for (; j >=0 ; j-=gap) {
                if (array[j]>tmp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    public static void selectSort(int[] array){

        for (int i = 0; i <array.length ; i++) {
            int minIndex =i;
            for (int j = i+1; j < array.length; j++) {
                if(array[minIndex]>array[j]){
                    minIndex=j;
                }
            }
            swap(array, i,  minIndex);
        }
    }

    public static void selectSort2(int[] array){
        int left =0;
        int right= array.length-1;
        while (left<right){
            int minIhdex = left;
            int maxIndex =left;
            for (int i =left+1 ; i <= right ; i++) {
                if(array[i]>array[maxIndex]){
                    maxIndex =i;
                }
                if (array[i]<array[minIhdex]){
                    minIhdex =i;
                }
            }
            swap(array,left,minIhdex);
            if(left == maxIndex){
                maxIndex=minIhdex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }




    public static void heapSort(int[] array){
        creatHeap(array);
        int size = array.length-1;
        while (size!=0){
            swap(array,0,size);
            shiftDown(array,0,size--);
        }
    }
    private static void creatHeap(int[] array){
        for (int i = (array.length-1-1)/2; i >=0 ; i--) {
            shiftDown(array,i, array.length);
        }
    }
    private static void shiftDown(int[] array,int pre,int end){
        int child =pre*2+1;
        while (child<end){
            if(child+1 < end && array[child]<array[child+1]){
                child=child+1;
            }
            if (array[pre]<array[child]){
                swap(array,pre,child);
                pre=child;
                child=pre*2+1;
            }else {
                break;
            }

        }
    }

    public static void bullueSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag =true;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    flag =false;
                    swap(array,j,j+1);
                }
            }
            if (flag){
                break;
            }
        }
    }


    public static void quickSort(int[] array){
        quickNor(array ,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end){
            return;
        }
        if (start-end+1<10){
            insertSortRange(array,start,end);
            return;
        }
        int middle = getMid(array,start,end);
        swap(array,start,middle);
        int pivot = paratition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    private static void quickNor(int[] array, int start, int end){
        Stack<Integer> stack =new Stack<>();
        int middle =paratition(array,start,end);
        if (middle>start+1){
            stack.push(start);
            stack.push(middle-1);
        }
        if (middle<end-1){
            stack.push(middle+1);
            stack.push(end);
        }
        while (!stack.isEmpty()){
            end =stack.pop();
            start = stack.pop();
            middle =paratition(array,start,end);
            if (middle>start+1){
                stack.push(start);
                stack.push(middle-1);
            }
            if (middle<end-1){
                stack.push(middle+1);
                stack.push(end);
            }
        }
    }
    private static void insertSortRange(int[] array,int start,int end ){
        for (int i = start+1; i <=end ; i++) {
            int tmp =array[i];
            int j = i-1;
            for (; j >=start ; j--) {
                if (array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1] =tmp;
        }
    }
    private static int getMid(int[] array, int left, int right) {
        int mid =(left+right)/2;
        if (array[left]<array[right]){
            if (array[mid]<array[left]){
                return left;
            }else if (array[mid]>array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid]>array[left]){
                return left;
            }else if (array[mid]<array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }

    private static int paratitionIndex(int[] array, int left, int right){
        int pre =left;
        int cur =left+1;
        int tmp =array[left];
        while (cur <= right){
            if (array[cur] <= tmp && array[++pre]!=array[cur]){
                swap(array,pre,cur);
            }
            cur++;
        }
        swap(array,left,pre);
        return pre;
    }
    private static int paratitionIndex2(int[] array, int left, int right) {
        int d = left + 1;
        int pivot = array[left];
        for (int i = left + 1; i <= right; i++) {
            if (array[i] < pivot) {
                swap(array, i, d);
                d++;
            }
        }
        swap(array, d - 1, left);

        return d - 1;
    }

    private static int paratition(int[] array, int left, int right) {
        int tmp =array[left];
        while (left<right){
            while (left<right &&array[right]>=tmp){
                right--;
            }
            array[left]=array[right];
            while (left<right &&array[left] <=tmp){
                left++;
            }
            array[right] =array[left];
        }
        array[left] =tmp;
        return left;
    }

    private static int paratitionHore(int[] array, int left, int right) {
        int tmp =array[left];
        int tmpleft =left;
        while (left<right){
            while (left<right &&array[right]>=tmp){
                right--;
            }
            while (left<right &&array[left] <=tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,tmpleft);
        return left;
    }



    public static void mergeSort(int[] array){
        mergeNor(array,0,array.length-1);
    }

    private static void merge(int[] array, int left, int right) {
        if (left>=right){
            return;
        }
        int midIndex =(left+right)/2;
        merge(array,0,midIndex);
        merge(array,midIndex+1,right);
        mergeArray(array,left,midIndex,right);
    }

    private static void mergeNor(int[] array, int left, int right){
        int gap =1;
        while (gap<array.length){
            for (int i = 0; i < array.length; i=i+gap*2) {
                int start =i;
                int mid =i+gap-1;
                if (mid>right){
                    mid=right;
                }
                int end =i+gap*2-1;
                if (end>right){
                    end =right;
                }
                mergeArray(array,start,mid,end);
            }
            gap*=2;
        }
    }

    private static void mergeArray(int[] array, int left,int minIndex,int right) {
        int len = right-left+1;
        int[] tmp =new int[len];
        int s1 =left;
        int s2 =minIndex+1;
        int index =0;
        while (s1<=minIndex  && s2<=right){
            if (array[s1]<=array[s2]){
                tmp[index++] =array[s1++];
            }else {
                tmp[index++] =array[s2++];
            }
        }
        while (s1<=minIndex){
            tmp[index++]=array[s1++];
        }
        while (s2<=right){
            tmp[index++] =array[s2++];
        }
        for (int i = 0; i <len; i++) {
            array[i+left]=tmp[i];
        }
    }


    public static void countSort(int[] array ){
        int minVal =array[0];
        int maxVal =array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i]>maxVal){
                maxVal=array[i];
            }
            if (array[i]<minVal){
                minVal =array[i];
            }
        }
        int range =maxVal-minVal+1;
        int[] tmp = new int[range];
        for (int i = 0; i < array.length; i++) {
            tmp[array[i]-minVal]++;
        }
        int Index =0;
        for (int i = 0; i < tmp.length; i++) {
            while (tmp[i]!=0){
                array[Index]=i+minVal;
                Index++;
                tmp[i]--;
            }
        }
    }

    private static void swap(int[] array, int i, int minIndex) {
        int tmp =array[i];
        array[i]=array[minIndex];
        array[minIndex]=tmp;
    }


}
